$(() => {
    function showGoods() {
        let goodsId = parseInt(getUrlParam('id'));
        if (isNaN(goodsId) || goodsId <= 0) {
            layer.msg('非法入侵,IP已被记录');
            setTimeout(() => {
                location.href = 'index.html';
            }, 2000);
            return;
        }
        let url = wurl + '/goods/detail';
        $.get(url, { goodsId }, response => {
            $('.summary>h3>strong').html(response.data.goodsName);
            $('.ml10>ul').html(`<li><span>商品编号：</span>${response.data.goodsNumber}</li><li class="market_price"><span>定价：</span><em>￥${response.data.marketPrice}</em></li><li class="shop_price"><span>本店价：</span> <strong>￥${response.data.shopPrice}</strong> <a href="">(降价通知)</a></li><li><span>上架时间：</span>${response.data.createTime}</li>`);
            let html = '';
            html = `<div class="jqzoom"><img src="${response.data.goodsImg[0].goodsThumb}"  alt="scarpa"  jqimg="${response.data.goodsImg[0].goodsThumb}" style="width:350px;height:350px; "></div>`
            $('.midpic').html(html);
            html = '';
            response.data.goodsImg.forEach((item, index) => {
                html += `<li class="${(index == 0) ? 'cur' : ''}"><a class="${(index == 0) ? 'zoomThumbActive' : ''}" href="javascript:void(0);" rel="{gallery: 'gal1', smallimage: '${item.goodsThumb}',largeimage: '${item.goodsThumb}'}"><img src="${item.goodsThumb}"></a></li>`;
            });
            $('.smallpic_wrap>ul').html(html);
            $(".jqzoom").jqueryzoom({
                xzoom: 500, //zooming div default width(default width value is 200)
                yzoom: 400, //zooming div default width(default height value is 200)
                offset: 10, //zooming div default offset(default offset value is 10)
                position: "right" //zooming div position(default position value is "right")
            });
        });
    }
    showGoods();

    $('.add_btn').on('click',function(){
        let goodsId = parseInt(getUrlParam('id'));
        // 记录这次加入购物车的商品数量
        let thisGoodsCount = parseInt($('.amount').val());
        // 之后记录数据库中已经有的当前商品的数量
        let preGoodsCount = 0;
        if(userInfo){
            // 用户已登录
            // 先查询用户购物车的数据
            $.ajax({
                url:wurl + '/cart/lists',
                data:{userId:userInfo.id},
                success:function(response){
                    // 有当前这个商品的话，就与这次加入的数量相加
                    response.data.forEach(item=>{
                        if(item.goodsId == goodsId){
                            preGoodsCount = item.goodsCount;
                        }
                    });
                    $.ajax({
                        url:wurl + '/cart/add',
                        type:'post',
                        contentType:'application/json',
                        data:JSON.stringify({userId:userInfo.id,goodsId,goodsCount:thisGoodsCount+preGoodsCount}),
                        success:function(response){
                            location.href = 'flow1.html';
                        }
                    });
                }
            });
        }else{
            // 用户未登录
            let userCart = localStorage.getItem('userCart')?JSON.parse(localStorage.getItem('userCart')):{};
            if(userCart[goodsId]){
                userCart[goodsId] = thisGoodsCount + parseInt(userCart[goodsId]);
            }else{
                userCart[goodsId] = thisGoodsCount;
            }
            localStorage.setItem('userCart',JSON.stringify(userCart));
            location.href = 'flow1.html';

        }

    });

});